// https://codeforces.com/problemset/problem/251/A

#include <iostream>
#include <algorithm>
using namespace std;
int n, d;
using LL = long long;
const int N = 100010;
int a[N];

LL get(int x)
{
    return (x + 1) * (LL)x / 2;
}

int main()
{
    cin >> n >> d;
    for(int i = 0; i < n; ++i) cin >> a[i];
    if(n < 3)
    {
        cout << 0 << endl;
        return 0;
    }
    int i = 0, j = 2;
    LL ans = 0;
    while(j < n)
    {
        while(i < j && a[j] - a[i] > d)
            ++i;
        if(i + 2 <= j)
            ans += get(j - i - 1);
        ++j;
    }
    cout << ans << endl;
    return 0;
}